package cn.lblbc.puzzle

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AlertDialog


open class MainActivity : ChoosePhotoActivity() {
    private var focusIndex = -1
    private var drawableMap = mutableMapOf<Int, Bitmap>()
    private lateinit var iv0: ImageView
    private lateinit var iv1: ImageView
    private lateinit var iv2: ImageView
    private lateinit var iv3: ImageView
    private lateinit var iv4: ImageView
    private lateinit var iv5: ImageView
    private lateinit var iv6: ImageView
    private lateinit var iv7: ImageView
    private lateinit var iv8: ImageView
    private lateinit var button: Button
    private var views: MutableList<ImageView> = mutableListOf()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_main)
        iv0 = findViewById(R.id.iv0)
        iv1 = findViewById(R.id.iv1)
        iv2 = findViewById(R.id.iv2)
        iv3 = findViewById(R.id.iv3)
        iv4 = findViewById(R.id.iv4)
        iv5 = findViewById(R.id.iv5)
        iv6 = findViewById(R.id.iv6)
        iv7 = findViewById(R.id.iv7)
        iv8 = findViewById(R.id.iv8)
        button = findViewById(R.id.button)
        views.add(iv0)
        views.add(iv1)
        views.add(iv2)
        views.add(iv3)
        views.add(iv4)
        views.add(iv5)
        views.add(iv6)
        views.add(iv7)
        views.add(iv8)
        for (i in 0 until views.size) {
            views[i].tag = i
        }
        iv0.setOnClickListener { onClickIcon(0) }
        iv1.setOnClickListener { onClickIcon(1) }
        iv2.setOnClickListener { onClickIcon(2) }
        iv3.setOnClickListener { onClickIcon(3) }
        iv4.setOnClickListener { onClickIcon(4) }
        iv5.setOnClickListener { onClickIcon(5) }
        iv6.setOnClickListener { onClickIcon(6) }
        iv7.setOnClickListener { onClickIcon(7) }
        iv8.setOnClickListener { onClickIcon(8) }

        button.setOnClickListener {
            val list = drawableMap.keys.shuffled()
            for (i in 0 until views.size) {
                val key = list[i]
                val drawable = drawableMap[key]!!
                views[i].setImageBitmap(drawable)
                views[i].tag = key
            }
        }

        val bitmap = BitmapFactory.decodeResource(resources, R.drawable.pic)
        initBitmap(bitmap)
    }

    override fun onRefreshBitmap(bitmap: Bitmap) {
        initBitmap(bitmap)
    }

    private fun initBitmap(bitmap: Bitmap) {
        val bitmaps = ArrayList<Bitmap>()
        val size = minOf(bitmap.width, bitmap.height) / 3
        for (i in 0..2) {
            for (j in 0..2) {
                val xValue = j * size
                val yValue = i * size
                val bitmap = Bitmap.createBitmap(bitmap, xValue, yValue, size, size)
                bitmaps.add(bitmap)
            }
        }

        for (i in 0..8) {
            drawableMap[i] = bitmaps[i]
        }

        for (i in 0 until views.size) {
            val drawable = drawableMap[i]!!
            views[i].setImageBitmap(drawable)
        }
    }

    private fun onClickIcon(index: Int) {
        if (focusIndex != -1) {
            if (focusIndex != index) {
                val focusIndexTag = views[focusIndex].tag
                val indexTag = views[index].tag

                views[focusIndex].setImageBitmap(drawableMap[indexTag]!!)
                views[focusIndex].tag = indexTag

                views[index].setImageBitmap(drawableMap[focusIndexTag]!!)
                views[index].tag = focusIndexTag

                checkResult()
            }
            focusIndex = -1
        } else {
            focusIndex = index
        }
        refreshIconBackground()
    }

    private fun checkResult() {
        var result = true
        for (i in 0 until views.size) {
            if (views[i].tag != null && views[i].tag != i) {
                result = false
            }
        }
        if (result) {
            showAlertDialog()
        }
    }

    private fun showAlertDialog() {
        val dialog = AlertDialog.Builder(this)
            .setMessage("恭喜，拼好啦！")
            .setPositiveButton("确定") { _, _ -> }
            .create()
        dialog.show()
    }

    private fun refreshIconBackground() {
        for (i in 0 until views.size) {
            val backgroundColor = getBackgroundColor(i)
            views[i].setBackgroundColor(backgroundColor)
        }
    }

    private fun getBackgroundColor(index: Int): Int {
        if (focusIndex == index) {
            return Color.YELLOW
        } else {
            return Color.WHITE
        }
    }
}